package otl;

import processing.core.*;
import processing.data.*;
import de.fhpotsdam.unfolding.UnfoldingMap;

import java.util.ArrayList;

//import processing.core.*;

@SuppressWarnings("serial")
public class Line extends PApplet {

	
	PApplet parent; // The parent PApplet that we will render ourselves onto
	UnfoldingMap map;
	
	Table stationTable;
	Table placesTable;
	ArrayList<Station> stationList;
	ArrayList<ArrayList<Place>> placesMatrix;
	
	//CONSTRUCTORS
	Line(UnfoldingMap m, PApplet p, Table sTable, Table pTable) {
		//println("building new Line");
		parent = p;
		map = m;
		buildStationList(sTable);
		buildPlacesMatrix(pTable);
	}
	
	public void drawLine(){
		//println("in Line's display...");
		drawStations();
	}
	
	private void buildStationList(Table sTable){
		stationList = new ArrayList<Station>(sTable.getRowCount());
		//create and add the station objects to the list
		for (TableRow row : sTable.rows()) {
		    int id = row.getInt("ID");
		    String stationName = row.getString("NAME");
		    String stationType = row.getString("TYPE");
		    float longitude = row.getFloat("LONG");
		    float latitude = row.getFloat("LAT");			    
		    Station thisStation = new Station(parent,map,id,stationName,longitude,latitude,stationType);
		    stationList.add(thisStation);
		}
		
	}
	
	private void buildPlacesMatrix(Table pTable){
		//println("building places");
		
		//placesList = new Vector<Object>(stationList.size());
		placesMatrix = new ArrayList<>();
		//instantiate a new placeList for each index of PlacesList
		for(int i = 0; i <= stationList.size(); i++) {   
			placesMatrix.add(new ArrayList<Place>());
		}
		
		for (TableRow row : pTable.rows()) {
		    int id = row.getInt("ID");
		    int stationID = row.getInt("stationID");
		    String placeName = row.getString("NAME");
		    String placeDType = row.getString("dTYPE");
		    float longitude = row.getFloat("LONG");
		    float latitude = row.getFloat("LAT");	
		    
		    Station thisStation = stationList.get(stationID-1);
		    Place thisPlace = new Place(thisStation.parent, thisStation.map,id,placeName,stationID, longitude,latitude, placeDType);
		    //println("trying to add " + placeName +" to station ID: " + stationID + "with long: " + longitude);
		    placesMatrix.get(stationID-1).add(thisPlace);
		}
		
		//NOW THAT WE'VE GONE THROUGH ALL OF THE pLACES IN THE FILE LETS ADD THE LISTS TO THE STATION OBJECTS
		for(int i = 0; i < stationList.size(); i++) {   
			stationList.get(i).setPlaces(placesMatrix.get(i));	
		}
		
	}
	
	private void drawStations(){
		
		for(int i = 0; i < stationList.size(); i++) {   
			Station thisStation = stationList.get(i);
			//println("station id " + (thisStation.id) + " has this many places " + thisStation.getNumPlaces());
			thisStation.drawStation();
		}
	}
}
